var app = getApp()
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: '0',
    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum: 1, //页数
    data: {
      deptName: '' //商家名
    },

    listnum: 10, //分页列表个数
  },

  getShopsList: function () {
    var that = this
    var pageNum = that.data.startNum
    var num = that.data.listnum
    var url = 'current=' + pageNum + '&size=' + num;
    var data = that.data.data
    util.req(api.NearbyShops + url, data, "").then(function (res) {
      wx.showLoading({
        title: '加载中',
      })
      if (res.success) {
        wx.hideLoading();
        var listSize = res.obj.records.length;
        var newData = that.data.myAgentList.list;
        var lis = res.obj.records;
        lis.forEach(function (item, index, array) {
          array[index] = {
            id: item.id,
            address: item.address,
            deptName: item.deptName,
            phone: item.phone,
            parentId: item.parentId,
            status: item.status,
          }
        });
        newData = lis;
        var total1 = res.obj.total;
        var newList = {};
        //当长度小于5表示没有下页数据了
        if (listSize < num) {
          newList.hasMore = false;
        } else {
          newList.hasMore = true;
        }
        newList.list = newData;
        newList.listSize = listSize;
        that.setData({
          myAgentList: newList,
          total: total1
        });
        that.setData({
          startNum: 2
        })
      } else {
        wx.hideLoading();
        console.log("获取全部商品列表失败")
      }
    }).catch(function (res) {
      console.log("获取数据失败！")
    })
  },

  onLoad() {
    var that = this;
    // 获取当前设备信息
    that.getShopsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})